package activities;

import org.json.JSONObject;

import com.evaluity.R;

import logic.User;
import logic.User.OnLoginUsuario;

import android.os.Bundle;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Login extends Activity {

    
    private Button btnLogin;
    private EditText inputEmail;
    private EditText inputPassword;
    private TextView loginErrorMsg;
    
    private Dialog crearDialogoAlerta()
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
     
        builder.setTitle("Connection not possible");
        builder.setMessage("Check your connection.");
        builder.setPositiveButton("OK", new OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
     
        return builder.create();
    }
    
    
    public void startMainActivity() {
		
		Intent i = new Intent(this, MainMenu.class);
		//i.putExtra(Constants.EXTRA_ACTIVITY, Constants.ASSISTENTS_ACTIVITY);
		startActivity(i);
	}
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        User usuario = new User();
        if(usuario.isUserLoggedIn(this)) startMainActivity();
        
      //Quitamos barra de titulo de la aplicacion
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        
      //Quitamos barra de notificaciones
      //this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        	
        setContentView(R.layout.activity_login);
        
        
        inputEmail = (EditText) findViewById(R.id.txtEmail);
        inputEmail.setText(getEmail(this));
        inputPassword = (EditText) findViewById(R.id.txtPass);
        inputPassword.setVisibility(View.GONE);
        btnLogin = (Button) findViewById(R.id.btnLogin);
        loginErrorMsg = (TextView) findViewById(R.id.login_error);
        loginErrorMsg.setText("Login or Signup with your email");
        
        btnLogin.setOnClickListener(new View.OnClickListener() {
 
            public void onClick(View view) {
                String email = inputEmail.getText().toString();
                String password = inputPassword.getText().toString();
                
                User usuario = new User();
                
                usuario.setOnLoginUsuario(new OnLoginUsuario() {
                	
                	@Override
                	public void onLoginWrong(String msg) {loginErrorMsg.setText(msg);}
                	
                	@Override
                	public void onUserExistDisplayPassField() {inputPassword.setVisibility(View.VISIBLE);}
                	
                	@Override
                	public void onNotInternetAllowed() {
                		crearDialogoAlerta().show();
                	}
                	
                	
                	@Override
                	public void onNewUser() {
                	
                		Context context = getApplicationContext();
                		CharSequence text = "Wellcome to Evaluity!";
                		int duration = Toast.LENGTH_SHORT;

                		Toast toast = Toast.makeText(context, text, duration);
                		toast.show();
                	}
                	
                	@Override
                	public void onLoginCorrect(JSONObject json, String msg) {
                		loginErrorMsg.setText("");
                		Intent itemintent = new Intent(Login.this, MainMenu.class);
                		Login.this.startActivity(itemintent);
                	}
                });                
                usuario.login(Login.this, email, password);
            }
        });
        
    }
    
    //Get account mail----
    
    static String getEmail(Context context) {
	    AccountManager accountManager = AccountManager.get(context); 
	    Account account = getAccount(accountManager);

	    if (account == null) {
	      return null;
	    } else {
	      return account.name;
	    }
    }

    private static Account getAccount(AccountManager accountManager) {
	    Account[] accounts = accountManager.getAccountsByType("com.google");
	    Account account;
	    if (accounts.length > 0) {
	      account = accounts[0];      
	    } else {
	      account = null;
	    }
	    return account;
    }
    
    //Fi get account mail----
 
    /*@Override
    public void onBackPressed() {
       Intent setIntent = new Intent(this, Login.class);
       setIntent.addCategory(Intent.CATEGORY_HOME);
       startActivity(setIntent);
    }*/
}